<?php

/**
 * @author Laurent SITTLER <lolo.sittler@gmail.com>
 * @author Alexandre GOMEZ <gomez.alexandre02@gmail.com>
 * @version 0.1
 */
require_once 'Library/Culture.php';

/**
 * User Model Class
 */
class User_Model extends Model {

    /**
     * Constructor
     */
    function __construct() {
        parent::__construct();
    }

    /**
     * Get all users
     * @return Array<User> Users collection
     */
    public function GetAllUsers() {
        $list = array();
        $rep = $this->db->query("SELECT * FROM HAB_SGBD_USER ORDER BY USER_ID");
        while ($result = $rep->fetch()) {
            $u = new User();
            $u->SetUser($result['USER_ID'], $result['USER_LOGIN'], $result['USER_FNM'], $result['USER_LNM'], $result['USER_CREATED'], $result['USER_LAST_LOG'], $result['USER_STATUS'], $result['USER_ADMIN']);
            array_push($list, $u);
        }
        $rep->closeCursor();
        return $list;
    }

    /**
     * Get an user by Id
     * @param Integer $id UserID
     * @return User
     */
    public function GetUserById($id) {
        $usr = new User();
        $req = $this->db->prepare("SELECT * FROM HAB_SGBD_USER WHERE USER_ID = :id");
        $req->execute(array(
            ":id" => $id
        ));
        $res = $req->fetchAll();
        $usr->SetUser($res[0]['USER_ID'], $res[0]['USER_LOGIN'], $res[0]['USER_FNM'], $res[0]['USER_LNM'], $res[0]['USER_CREATED'], $res[0]['USER_LAST_LOG'], $res[0]['USER_STATUS'], $res[0]['USER_ADMIN']);
        $req->closeCursor();
        return $usr;
    }

    /**
     * Get User ID login
     * @param String $login User login
     * @param String $password User password
     * @return Integer User ID
     */
    public function GetUserLogin($login, $password) {
        $usr = $this->db->prepare("SELECT * FROM HAB_SGBD_USER WHERE
                      USER_LOGIN LIKE :login AND USER_PASSWORD LIKE :password", array(PDO::ATTR_CURSOR,
            PDO::CURSOR_SCROLL));
        $usr->execute(array(
            ":login" => $login,
            ":password" => md5($password)
        ));
        $res = $usr->fetchAll();
        $usr->closeCursor();
        return $res[0]['USER_ID'];
    }

    /**
     * Get number of user selected by Login and Password
     * @param type $login User login
     * @param type $password User password
     * @return Integer User ID
     */
    public function CountUser($login, $password) {
        $usr = $this->db->prepare("SELECT count(*) as NB_USER FROM HAB_SGBD_USER WHERE
                      USER_LOGIN LIKE :login AND USER_PASSWORD LIKE :password");
        $usr->execute(array(
            ":login" => $login,
            ":password" => md5($password)
        ));
        $res = $usr->fetchAll();
        $usr->closeCursor();
        return $res[0]['NB_USER'];
    }
    
    /**
     * 
     * @param type $id
     * @return type
     */
    public function GetStatusbyId($id) {
        $usr = $this->db->prepare("SELECT * FROM HAB_SGBD_USER WHERE USER_ID LIKE :id");
        $usr->execute(array(
            ":id" => $id
        ));
        $res = $usr->fetchAll();
        $usr->closeCursor();
        return $res[0]['USER_STATUS'];
    }

    /**
     * Update Last log date user
     * @param Interger $id User ID
     */
    public function UpdateLastLog($id) {
        $req = $this->db->prepare("UPDATE HAB_SGBD_USER SET USER_LAST_LOG = to_date(:data, 'DD-MM-YYYY HH24:MI')
                    WHERE USER_ID = :id");
        $req->execute(array(
            ":id" => $id,
            ":data" => date('d-m-Y H:m')
        ));
        $req->closeCursor();
    }
    
    /**
     * Update password user id
     * @param Interger $id
     * @param String $password
     */
    public function UpdatePassword($id, $password){
        $req = $this->db->prepare("UPDATE HAB_SGBD_USER SET USER_PASSWORD = :pwd WHERE USER_ID = :id");
        $req->execute(array(
            ":id" => $id,
            ":pwd" => md5($password)
        ));
        $req->closeCursor();
        Session::Set(Constants::$SESSION_valid, "Mot de passe changer!");
    }
    
    /**
     * Insert new user
     * @param String $login User login
     * @param String $pwd User password
     * @param String $fnm User first name
     * @param String $lnm User last name
     * @param Integer $status User status account
     * @param Integer $admin User rule interface
     * @return Last ID insert
     */
    public function InsertUser($login, $pwd, $fnm, $lnm, $status, $admin){
        $clog = $this->db->prepare("SELECT hab_sgbd_user_seq.NEXTVAL FROM dual");
        $clog->execute();
        $lastInsert = $clog->fetchAll();
        $clog->closeCursor();
        
        $req = $this->db->prepare("INSERT INTO HAB_SGBD_USER (USER_ID, USER_LOGIN, USER_PASSWORD, USER_FNM, USER_LNM, USER_CREATED, USER_STATUS, USER_ADMIN) VALUES (:id, :login,  :pwd, :fname, :lname, to_date(:dcreate, '" . Culture::FormatDatePDO() . "'), :status, :rule)");
        $req->execute(array(
            ":id" => 0,
            ":login" => $login,
            ":pwd" => md5($pwd),
            ":fname" => $fnm,
            ":lname" => $lnm,
            ":dcreate" => date('d-m-Y H:m'),
            ":status" => $status,
            ":rule" => $admin
        ));
        $req->closeCursor();
        return $lastInsert[0]['NEXTVAL'] + 1;
    }
    
    /**
     * Delete an user
     * @param Integer $id User ID
     */
    public function DeleteUser($id){
        $dbUser = new Database_Model();
        $dbUser->DeleteAllDBUser($id);
        $req = $this->db->prepare("DELETE FROM HAB_SGBD_USER CASCADE WHERE USER_ID LIKE :id");
        $req->execute(array(
            ":id" => $id
        ));
        // print_r($req->errorInfo());
        $req->closeCursor();
    }

    /**
     * Update Log informations
     * @param Integer $userId
     * @param String $userIp
     * @param String $msg
     */
    public function User_Log($userId, $userIp, $msg){
        $req = $this->db->prepare("INSERT INTO APP_INT_LOG (LOG_ID, LOG_DATE, LOG_IP, LOG_MSG, LOG_USER_ID) VALUES (:id, to_date(:dcreate, '" . Culture::FormatDatePDO() . "'), :userip,  :msg, :userid)");
        $req->execute(array(
            ":id" => 0,
            ":dcreate" => date('d-m-Y H:m'),
            ":userip" => $userIp,
            ":msg" => $msg,
            ":userid" => $userId
        ));
        echo $msg;
        $req->closeCursor();
    }
}
